package com.wanmait.teatrain.config;

import com.wanmait.teatrain.interceptor.InitInterceptor;
import com.wanmait.teatrain.interceptor.JWTinterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;


@Configuration
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private JWTinterceptor jwTinterceptor;

    @Resource
    private InitInterceptor initInterceptor;

//    //拦截器配置
//    @Override
//    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(jwTinterceptor).addPathPatterns("/**")
//                .excludePathPatterns("/common/code","/common/login")
//                .excludePathPatterns("/static/**");
//        registry.addInterceptor(initInterceptor).addPathPatterns("/**");
//    }
// 拦截器配置
@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(jwTinterceptor)
            .addPathPatterns("/**") // 拦截所有路径
            .excludePathPatterns(
                    "/common/code",
                    "/common/login",
                    "/static/**",
                    "/documents/**" // 新增：排除 PDF 所在的 /documents/ 路径（关键）
            );
    registry.addInterceptor(initInterceptor).addPathPatterns("/**");
}


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //允许哪些前端的地址可以访问后端的接口
                .allowedOriginPatterns("*")
                //运行可以携带哪些请求头
                .allowedHeaders("*")
                //允许哪些方法可以访问
                .allowedMethods("*")
                //凭证，如果设为true，如果全后端不一样
                .allowCredentials(true)
                .maxAge(3600);
    }
    //配置静态资源
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置静态资源映射
        registry.addResourceHandler("/static/**")
                .addResourceLocations("classpath:/static/");
    }
}
